package boxuegu.com.boxuegu2.utils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

import boxuegu.com.boxuegu2.bean.VideoBean;
import boxuegu.com.boxuegu2.sqlite.SQLiteHelper;

public class DBUtils {
    private SQLiteDatabase saveDB;
    private SQLiteDatabase readDB;
    private  static DBUtils instance=null;

    private DBUtils() {
    }

    private DBUtils(Context context) {
        SQLiteHelper helper=new SQLiteHelper(context);
        saveDB=helper.getWritableDatabase();
        readDB=helper.getReadableDatabase();
    }

    public static DBUtils getInstance(Context context){
        if(instance==null){
            instance=new DBUtils(context);
        }
        return instance;
    }

    public void saveVideoList(VideoBean bean,String name){
        //如果当前这条视频已经有播放记录，则要删除原有记录，保存新记录，每一条视频只保存最新播放记录
        if(isVideoPlay(bean.id,bean.videoId,name)){
            if(!delVideoList(bean.id,bean.videoId,name))
                return;
        }

        //保存播放记录
        ContentValues cv=new ContentValues();
        cv.put("userName",name);
        cv.put("id",bean.id);
        cv.put("videoId",bean.videoId);
        cv.put("videoPath",bean.videoPath);
        cv.put("title",bean.title);
        cv.put("videoTitle",bean.videoTitle);
        saveDB.insert("videoplaylist",null,cv);
    }

    public boolean isVideoPlay(int id,int videoId,String name){//检查是否存在某条视频的播放记录
        boolean has=false;
        String sql="select * " +
                "from videoplaylist " +
                "where id=? and videoId=? and userName=?";
        Cursor cursor=readDB.rawQuery(sql,new String[]{id+"",videoId+"",name});
        //游标集合指针指向第一条前面
        if(cursor.moveToFirst()){
            has=true;
        }
        cursor.close();
        return has;
    }

    public boolean delVideoList(int id,int videoId,String name){
        int row=saveDB.delete("videoplaylist"," id=? and videoId=? and userName=?",new String[]{id+"",videoId+"",name});
        if(row>0){
            return true;
        }else {
            return false;
        }
    }

    public List<VideoBean> getPlayHistory(String name){
        String sql="select * " +
                "from videoplaylist " +
                "where userName=?";
        Cursor cursor=readDB.rawQuery(sql,new String[]{name});
        //游标集合指针指向第一条前面
        List<VideoBean> vbl = new ArrayList<VideoBean>();
        while (cursor.moveToNext()) {
            VideoBean bean = new VideoBean();
            bean.id=cursor.getInt(cursor.getColumnIndex("id"));
            bean.videoId=cursor.getInt(cursor.getColumnIndex("videoId"));
            bean.videoPath=cursor.getString(cursor
                    .getColumnIndex("videoPath"));
            bean.title=cursor.getString(cursor.getColumnIndex("title"));
            bean.videoTitle=cursor.getString(cursor
                    .getColumnIndex("videoTitle"));
            vbl.add(bean);
        }
        cursor.close();
        return vbl;
    }
}
